package GUI;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import MessagePacks.GetAllPack;
import MessagePacks.MessagePack;
import MessagePacks.UpdatePack;
import MessagePacks.getCommitteePack;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JTextField;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class SupervisorMainScreen extends MainScreenEngineer{

	private static final long serialVersionUID = 1L;
	private MainGUI maingui;
	private JButton btnAproveEstimator;
	private JPanel panel_1;
	private JPanel panel;
	private JComboBox<String> procBox;
	private JButton btnAprove;
	private JComboBox<String> estBox;
	private JButton btnChange;
	private GetAllPack getWorkers;
	private GetAllPack getTime;
	private UpdatePack updStst;
	private UpdatePack changeEst;
	private UpdatePack activeStat;
	private UpdatePack updatePhs;
	private UpdatePack upExecflag;
	private JButton btnAproveEstimationTime;
	private JPanel panel_2;
	private JLabel lblTimeRequested;
	private JTextField textField;
	private JButton btnApproved;
	private JButton btnNotApproved;
	private int time;
	private JButton btnEndProcess;
	private JButton btnSuspendProcess;
	private JPanel panel_3;
	private JComboBox<String> execLeaderBox;
	private JButton btnDesignate;
	private JButton btnDesignetExecLeader;
	private getCommitteePack getComm;
	private JTextField textField_1;
	private JButton btnAproveTimeExt;
	private JPanel panel_4;
	private JLabel lblTimeRequested_1;
	private JTextField textField_2;
	private JButton btnApproved_1;
	private JButton btnNotApproved_1;
	private GetAllPack getextrqst;
	private UpdatePack updtTime;
	
	

	public SupervisorMainScreen(MainGUI mainGui) {
		super(mainGui);
		this.maingui=mainGui;

		getworkers();
		getCommMembers();

		
		panel = new JPanel();
		panel.setBounds(0, 264, 176, 216);
		getContentPane().add(panel);
		panel.setLayout(null);

		setBtnAproveEstimationTime();
		setBtnAproveEstimator();
		
		panel_1 = new JPanel();
		panel_1.setBounds(215, 118, 542, 48);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		panel_1.setVisible(false);
				
		JLabel label = new JLabel("Estimator ID");
		label.setFont(new Font("Tahoma", Font.BOLD, 14));
		label.setBounds(10, 11, 122, 20);
		panel_1.add(label);
				
		JLabel lblProcessId = new JLabel("Process ID");
		lblProcessId.setBounds(234, 71, 122, 20);
		getContentPane().add(lblProcessId);
		lblProcessId.setHorizontalAlignment(SwingConstants.CENTER);
		lblProcessId.setFont(new Font("Tahoma", Font.BOLD, 14));
		
		procBox = new JComboBox<String>();
		procBox.setBounds(366, 71, 122, 20);
		getContentPane().add(procBox);
		procBox.setVisible(true);
		
		panel_2 = new JPanel();
		panel_2.setBounds(215, 177, 542, 48);
		getContentPane().add(panel_2);
		panel_2.setLayout(null);
		panel_2.setVisible(false);
		
		lblTimeRequested = new JLabel("Time requested");
		lblTimeRequested.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTimeRequested.setBounds(10, 11, 122, 26);
		panel_2.add(lblTimeRequested);
						
		textField = new JTextField();
		textField.setBounds(194, 16, 86, 20);
		panel_2.add(textField);
		textField.setColumns(10);
		
		panel_3 = new JPanel();
		panel_3.setBounds(215, 241, 542, 48);
		getContentPane().add(panel_3);
		panel_3.setLayout(null);
		panel_3.setVisible(false);
		
		JLabel lblDesignateExecLeader = new JLabel("Designate Exec. Leader : ");
		lblDesignateExecLeader.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblDesignateExecLeader.setBounds(10, 11, 179, 26);
		panel_3.add(lblDesignateExecLeader);
		
		textField_1 = new JTextField();
		textField_1.setBounds(327, 16, 86, 20);
		panel_3.add(textField_1);
		textField_1.setColumns(10);
		
		execLeaderBox = new JComboBox();
		execLeaderBox.addItem("");
		setMembers();
		execLeaderBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				int selIt = Integer.parseInt((String)execLeaderBox.getSelectedItem());
				Iterator<Entry<String, String>> iter2 = getComm.getMyHashMap().entrySet().iterator();
				while (iter2.hasNext()){
					Map.Entry mEntry = (Map.Entry) iter2.next();
					int value =Integer.parseInt((String) mEntry.getKey());
					if(selIt==value){
						textField_1.setText((String) mEntry.getValue());
						textField_1.setEditable(false);
					}
					
				}
				
			}
		});
		execLeaderBox.setBounds(199, 16, 68, 20);
		panel_3.add(execLeaderBox);
		
		panel_4 = new JPanel();
		panel_4.setBounds(215, 300, 542, 48);
		getContentPane().add(panel_4);
		panel_4.setLayout(null);
		panel_4.setVisible(false);
			
		lblTimeRequested_1 = new JLabel("Time requested : ");
		lblTimeRequested_1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblTimeRequested_1.setBounds(10, 11, 160, 26);
		panel_4.add(lblTimeRequested_1);
			
			
		textField_2 = new JTextField();
		textField_2.setBounds(201, 16, 86, 20);
		panel_4.add(textField_2);
		textField_2.setColumns(10);
			
		
		
		setBtnAproveTimeExt();
		setBtnNotApproved_1();
		setBtnApproved_1();
		setBtnDesignetExecLeader();
		setBtnSuspendProcess();
		setBtnNotApproved();
		setBtnEndProcess();
		setBtnApproved();
		setProcBox();
		setBtnAprove();
		setEstBox(); 
		setBtnChange();
		setTextBox();
		setBtnDesignate();

	}



	public JButton getBtnAproveEstimator() {
		return btnAproveEstimator;
	}



	public void setBtnAproveEstimator() {
		btnAproveEstimator = new JButton("Aprove Estimator");
		btnAproveEstimator.setBounds(10, 11, 156, 23);
		panel.add(btnAproveEstimator);
		btnAproveEstimator.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panel_1.setVisible(true);			
			}
		});
		btnAproveEstimator.setEnabled(false);
		

	}



	public JComboBox<String> getEstBox() {
		return estBox;
	}



	public void setEstBox() {
		estBox = new JComboBox<String>();
		String toBox = ((Integer)maingui.process.getestimatorID()).toString();
		String procSupId = ((Integer)maingui.process.getsupervisorID() ).toString();
		estBox.addItem(toBox);
		Iterator<String> iter = getWorkers.nArray.iterator();
		
		while(iter.hasNext()){
			String toBox2 = iter.next();
			if(!toBox2.equals(toBox)&&!toBox2.equals(procSupId)){
				estBox.addItem(toBox2);
			}			
		}
		estBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				btnAprove.setEnabled(false);
				btnChange.setEnabled(true);		
			}
		});
		estBox.setBounds(195, 13, 102, 20);
		panel_1.add(estBox);
	}



	public JButton getBtnAprove() {
		return btnAprove;
	}



	public void setBtnAprove() {
		btnAprove = new JButton("Approve");
		btnAprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Active");
				updateRes();
				updatePhase();
				JOptionPane.showMessageDialog(serverGUI.mainframe,"New Estimator approved");
				panel_1.setVisible(false);
				btnAproveEstimator.setEnabled(false);
				
				
			}
		});
		btnAprove.setBounds(318, 12, 89, 23);
		panel_1.add(btnAprove);
	}



	public JButton getBtnChange() {
		return btnChange;
	}



	public void setBtnChange() {
		btnChange = new JButton("Change");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Active");
				updateRes();
				changeEstimator();
				updatePhase();
				JOptionPane.showMessageDialog(serverGUI.mainframe,"New Estimator changed");
				panel_1.setVisible(false);
				btnAproveEstimator.setEnabled(false);
			}
		});
		btnChange.setBounds(437, 12, 89, 23);
		btnChange.setEnabled(false);
		panel_1.add(btnChange);
	}



	public JComboBox<String> getProcBox() {
		return procBox;
	}




	public void setProcBox() {
		String toBox = ((Integer)maingui.process.getprocessID()).toString();
		procBox.addItem(toBox);
		if(maingui.process.getStatus().equals("waiting for approval")){
			btnAproveEstimator.setEnabled(true);
			}
	}
	
	
	public void setTextBox() {
		if(maingui.process.getStatus().equals("Waiting For Time Approval")){
			btnAproveEstimationTime.setEnabled(true);
			this.getProsTime();
			time = getTime.getNumber();
			System.out.println(time);
			String toTextBox = ((Integer)time).toString();
			textField.setText(toTextBox);
			textField.setEditable(false);
			}
	}
	
	
	public void getworkers(){
		getWorkers= new GetAllPack(MessagePack.getAllWorkers);
		maingui.client.handleMessageFromClientUI(getWorkers);
		getWorkers= (GetAllPack) maingui.client.getMessage();	
	}
	
	
	public void updateRes(){
		updStst= new UpdatePack(MessagePack.updresponsible);
		updStst.setProcId(Integer.parseInt((String) procBox.getSelectedItem()));
		updStst.setResponOk(0);
		maingui.client.handleMessageFromClientUI(updStst);
		
	}
	
	
	public void changeEstimator(){
		changeEst= new UpdatePack(MessagePack.updateEstimator);
		changeEst.setProcId(Integer.parseInt((String)  procBox.getSelectedItem()));
		changeEst.setWorkerId(Integer.parseInt((String) estBox.getSelectedItem()));
		maingui.client.handleMessageFromClientUI(changeEst);
		
	}
	

	public void updateStatus(String status){
		activeStat = new UpdatePack(MessagePack.updateStatus);
		activeStat.setStatus(status);
		activeStat.setProcId(maingui.process.getprocessID());
		maingui.client.handleMessageFromClientUI(activeStat);
	}
	

	public void updatePhase(){
		updatePhs = new UpdatePack(MessagePack.updatePhase);
		updatePhs.setPhaseNum(1);
		updatePhs.setProcId(maingui.process.getprocessID());
		maingui.client.handleMessageFromClientUI(updatePhs);
		upExecflag = new UpdatePack(MessagePack.updateExecTimeFlag);
		upExecflag.setProcId(maingui.process.getprocessID());
		upExecflag.setResponOk(0);
		maingui.client.handleMessageFromClientUI(upExecflag);
	}
	
	

	public JButton getBtnAproveEstimationTime() {
		return btnAproveEstimationTime;
	}
	
	

	public void setBtnAproveEstimationTime() {
		btnAproveEstimationTime = new JButton("Aprove estimation time");
		btnAproveEstimationTime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_2.setVisible(true);
			}
		});
		btnAproveEstimationTime.setEnabled(false);
		btnAproveEstimationTime.setBounds(10, 45, 156, 23);
		panel.add(btnAproveEstimationTime);
	}



	public JButton getBtnApproved() {
		return btnApproved;
	}



	public void setBtnApproved() {
		btnApproved = new JButton("Approved");
		btnApproved.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Active");
				updStst= new UpdatePack(MessagePack.updresponsible);
				updStst.setProcId(Integer.parseInt((String) procBox.getSelectedItem()));
				updStst.setResponOk(1);
				maingui.client.handleMessageFromClientUI(updStst);
				panel_2.setVisible(false);
				btnAproveEstimationTime.setEnabled(false);
				
			}
		});
		btnApproved.setBounds(322, 15, 89, 23);
		panel_2.add(btnApproved);
	}



	public JButton getBtnNotApproved() {
		return btnNotApproved;
	}



	public void setBtnNotApproved() {
		btnNotApproved = new JButton("Not approved");
		btnNotApproved.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_2.setVisible(false);
				btnAproveEstimationTime.setEnabled(false);
			}
		});
		btnNotApproved.setBounds(431, 15, 101, 23);
		panel_2.add(btnNotApproved);
	}
	
	
	public void getProsTime(){
		getTime = new GetAllPack(MessagePack.getTime);
		getTime.setInitID(maingui.process.getprocessID());
		maingui.client.handleMessageFromClientUI(getTime);
		getTime= (GetAllPack) maingui.client.getMessage();
		
	}



	public JButton getBtnEndProcess() {
		return btnEndProcess;
	}



	public void setBtnEndProcess() {
		btnEndProcess = new JButton("End process");
		if(maingui.process.getCurrentPhase()==5 && maingui.process.getStatus().equals("Active"))
			btnEndProcess.setEnabled(true);
		else
			btnEndProcess.setEnabled(false);
		btnEndProcess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Ended");
				btnEndProcess.setEnabled(false);
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Process ended.");
				
			}
		});

		btnEndProcess.setBounds(10, 147, 156, 23);
		panel.add(btnEndProcess);
		

		

	}



	public JButton getBtnSuspendProcess() {
		return btnSuspendProcess;
	}



	public void setBtnSuspendProcess() {
		btnSuspendProcess = new JButton("Suspend process");
		btnSuspendProcess.setEnabled(true);
		btnSuspendProcess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnAproveEstimator.setEnabled(false);
				btnAproveEstimationTime.setEnabled(false);
				updateStatus("Suspended");
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Process Suspended.");
				btnSuspendProcess.setEnabled(false);
			}
		});
		btnSuspendProcess.setBounds(10, 113, 156, 23);
		panel.add(btnSuspendProcess);
	}



	public JButton getBtnDesignate() {
		return btnDesignate;
	}



	public void setBtnDesignate() {
		btnDesignate = new JButton("Designate");
		btnDesignate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateRes();
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Exec. Leader designated.");
				updateExecleader();
				updateStatus("waiting for approval");
				panel_3.setVisible(false);
				btnDesignetExecLeader.setEnabled(false);
			}


		});
		btnDesignate.setBounds(443, 15, 89, 23);
		panel_3.add(btnDesignate);
		

	}



	public JButton getBtnDesignetExecLeader() {
		return btnDesignetExecLeader;
	}



	public void setBtnDesignetExecLeader() {
		btnDesignetExecLeader = new JButton("Designet Exec. leader");
		if(maingui.process.getStatus().equals("waiting For approval") && maingui.process.getCurrentPhase()==3){
			btnDesignetExecLeader.setEnabled(true);
		}
		else{
			btnDesignetExecLeader.setEnabled(false);
		}
		btnDesignetExecLeader.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_3.setVisible(true);				
			}
		});
		btnDesignetExecLeader.setBounds(10, 79, 156, 23);
		panel.add(btnDesignetExecLeader);
	}
	
	private void getCommMembers(){
		getComm = new getCommitteePack();
		maingui.client.handleMessageFromClientUI(getComm);
		getComm= (getCommitteePack) maingui.client.getMessage();
	}
	
	public void setMembers() {
		Iterator<Entry<String, String>> iter = getComm.getMyHashMap().entrySet().iterator();
		while (iter.hasNext()){
			Map.Entry mEntry = (Map.Entry) iter.next();
			execLeaderBox.addItem((String) mEntry.getKey());
			
		}
	
	}
	
	private void updateExecleader() {
		updatePhs = new UpdatePack(MessagePack.updateExecLeader);
		updatePhs.setWorkerId(Integer.parseInt((String) execLeaderBox.getSelectedItem()));
		updatePhs.setProcId(maingui.process.getprocessID());
		maingui.client.handleMessageFromClientUI(updatePhs);
		
	}



	public JButton getBtnAproveTimeExt() {
		return btnAproveTimeExt;
	}



	public void setBtnAproveTimeExt() {
		btnAproveTimeExt = new JButton("Aprove time Ext. Request");
		if(maingui.process.getStatus().equals("Waiting For Time Approval") && getflag()==0){
			btnAproveTimeExt.setEnabled(true);
			textField_2.setText(String.valueOf(getextrqst.getDays()));
			textField_2.setEditable(false);
		}
		else{
			btnAproveTimeExt.setEnabled(false);
		}
		btnAproveTimeExt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel_4.setVisible(true);
				btnAproveTimeExt.setEnabled(false);
				}
		});
		btnAproveTimeExt.setBounds(10, 181, 156, 23);
		panel.add(btnAproveTimeExt);
	}



	private int getflag() {
		getextrqst = new GetAllPack(MessagePack.getExtensionRequest);
		getextrqst.setInitID(maingui.process.getprocessID());
		getextrqst.setPhase(maingui.process.getCurrentPhase());
		maingui.client.handleMessageFromClientUI(getextrqst);
		getextrqst = (GetAllPack) maingui.client.getMessage();
		return getextrqst.getFlag();
	}
	
	private void updateTime(){
		updtTime = new UpdatePack(MessagePack.addTimeExtension);
		updtTime.setProcId(maingui.process.getprocessID());
		updtTime.setTime(Integer.parseInt(textField_2.getText()));
		maingui.client.handleMessageFromClientUI(updtTime);
	}



	public JButton getBtnApproved_1() {
		return btnApproved_1;
	}



	public void setBtnApproved_1() {
		btnApproved_1 = new JButton("Approved");
		btnApproved_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Active");
				updateTime();
				panel_4.setVisible(false);
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Time extension approved.");
				
			}
		});
		btnApproved_1.setBounds(315, 15, 89, 23);
		panel_4.add(btnApproved_1);
	}



	public JButton getBtnNotApproved_1() {
		return btnNotApproved_1;
	}



	public void setBtnNotApproved_1() {
		btnNotApproved_1 = new JButton("Not approved");
		btnNotApproved_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus("Active");
				panel_4.setVisible(false);
				JOptionPane.showMessageDialog(serverGUI.mainframe,"Time extension not approved.");
			}
		});
		btnNotApproved_1.setBounds(423, 15, 109, 23);
		panel_4.add(btnNotApproved_1);
	}
}
